﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Design;

namespace _0812367_0912581
{
    public class MySprite
    {
        private Texture2D textureImage;//co the chua 1 hay nhieu day hinh

        public Texture2D TextureImage
        {
            get { return textureImage; }
            set { textureImage = value; }
        }

        private Point sheetSize;//Point(so luong hinh tren 1 day, so day hinh tren textureImage)

        public Point SheetSize
        {
            get { return sheetSize; }
            set { sheetSize = value; }
        }

        private Point frameSize;//Point(chieu rong, chieu cao)

        public Point FrameSize
        {
            get { return frameSize; }
            set { frameSize = value; }
        }

        private Point currentFrame;

        public Point CurrentFrame
        {
            get { return currentFrame; }
            set { currentFrame = value; }
        }

        private Vector2 position;

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }
        
        private int collisionOffset;//giam kich thuoc collision

        public int CollisionOffset
        {
            get { return collisionOffset; }
            set { collisionOffset = value; }
        }



        private float scale = 1.0f;

        public float Scale
        {
            get { return scale; }
            set { scale = value; }
        }


        protected int timeSinceLastFrame = 0;
        protected int timePerFrame;

        public MySprite(Texture2D aImage, Point aSheetsize, Vector2 thePosition, int theColOffset, float scale, int timePerFrame)
        {
            this.textureImage = aImage;
            this.sheetSize = aSheetsize;
            this.frameSize = new Point(textureImage.Width / aSheetsize.X, textureImage.Height / aSheetsize.Y);
            this.currentFrame = new Point(0, 0);//***
            this.position = thePosition;
            this.collisionOffset = theColOffset;
            this.Scale = scale;
            this.timePerFrame = timePerFrame;
        }

        public MySprite(MySprite aMySprite)
        {
            this.textureImage = aMySprite.textureImage;
            this.sheetSize = aMySprite.sheetSize;
            this.frameSize = aMySprite.frameSize;
            this.currentFrame = aMySprite.currentFrame;
            this.position = aMySprite.position;
            this.collisionOffset = aMySprite.collisionOffset;
            this.Scale = aMySprite.Scale;
            this.timePerFrame = aMySprite.timePerFrame;

        }

        public virtual void LoadContent(ContentManager aContent)
        {

        }

        public virtual Rectangle collisionRect
        {
            get
            {
                return new Rectangle(
                (int)(position.X + collisionOffset * Scale),
                (int)(position.Y + collisionOffset * Scale),
                (int)(frameSize.X - collisionOffset * Scale),
                (int)(frameSize.Y - collisionOffset * Scale));
            }
        }

        public virtual void Update(GameTime gameTime)
        {
            //Update animation frame
            timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
            if (timeSinceLastFrame > timePerFrame)
            {
                timeSinceLastFrame = 0;
                ++currentFrame.X;
                if (currentFrame.X >= sheetSize.X)
                {
                    currentFrame.X = 0;
                    ++currentFrame.Y;
                    if (currentFrame.Y >= sheetSize.Y)
                        currentFrame.Y = 0;
                }
            }
        }

        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            //Draw the sprite
            spriteBatch.Draw(textureImage,
                position,
                new Rectangle(currentFrame.X * frameSize.X,
                    currentFrame.Y * frameSize.Y,
                    frameSize.X, frameSize.Y),
                Color.White, 0, Vector2.Zero,
                Scale, SpriteEffects.None, 0);

        }

        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch, Color aColor)
        {
            spriteBatch.Draw(textureImage, position, new Rectangle(currentFrame.X * frameSize.X, currentFrame.Y * frameSize.Y,
                frameSize.X, frameSize.Y), aColor, 0, Vector2.Zero, Scale, SpriteEffects.None, 0);
            
        }
       
        



    }
}
